/**
 * 功能描述
 */
define(function (require, exports, module) {
    //引入视图基类
    var ELView = require("elview");

    //assets
    var template = require("./assets/tpl/dishmenu_main.tpl");
    require("./assets/css/dishmenu_main.css");

    //business

    //component
    var dishMenuSubnavComp = require("./component/dishMenuSubnavComp");
    //constant

    //handler
    var events = require("./handler/events");
    //store
    
    //tpl需要
    var aw = require("ajaxwrapper");
    var Dialog = require("dialog");
    var Button = require("button");
    var Form = require("form");
    var store =require("store");
	var activeUser = store.get("user");
	
    var date = moment();
    
    var App = ELView.extend({
        attrs: {
        	model:{
        		
        	},
            template: template
        },
        date : moment(),
       
        events: {
        	"click .J-grid-1-edit": function(e){
        		var id = event.target.id.slice(4);
        		var widget = this;
        		var time = moment(Number(id)).startOf("day").valueOf();
        		
        		var remarkForm = new Form({
    				parentNode:".J-dialog-conponent",
    				autoRender:false,
    				model:{
    					defaultButton:false,
    					  items: [{
    				            name: "pkDish",
    				            type: "select",
    				            validate:"required",
    				            label: "请选菜品",
    				            url:"api/cater/dish/query",
    				            multi:true,
    				            key:"pkDish",
    							value:"name",
    				            style:{
    				            	value:"width:75%"
    				            }
    					  		}]
    					}	       			            	
    			});
    			//widget.set("remarkForm",remarkForm);
    			var dialog=Dialog.showComponent(remarkForm,{
    				defaultButton:false,
    				title:"菜单设置",
    				setStyle: function(){
    					$(".modal").css({
    						"width":"550px"
    					});
    				},
    				
    				buttons:[{
    					id:"save",
    					text:"保存",
    					className:"btn-primary",
    					handler:function(){
    						if(remarkForm.valid()){
    							var FormData = remarkForm.getData();
    							var str;
    							for(var i=0;i<FormData.pkDish.length;i++){
    								if(i==0){
    									str = FormData.pkDish[0];
    								}else{
    									str = str + "," + FormData.pkDish[i];
    								}
    							}
    							if(str==null){
    								return;
    							}
    							aw.ajax({
    		        				url: "api/cate/dishmenu/saveoneday",
    		        				data: {
    		        					"servicePoint":activeUser.servicePoint.pkServicePoint,
    		        					"menuDate":time,
    		        					"menuDateEnd":moment(time).endOf("day").format("x"),
    		        					"string":str,
    		        				},
    		        				dataType: "json",
    		        				success: function(data){
    		        					events.setData(widget,moment(time));
    		        					//window.location.reload();
    		        					Dialog.close();
    		        				}
    		        			})
    		        			Dialog.close();
    							Dialog.loading(false);
    							
    						};
    					}
    				},{
    					id:"close",
    					text:"取消",
    					handler:function(){
    						Dialog.close();
    					}
    				}]
    				})
        		
        		
        	},
        	
	        "click .J-grid-1-delete": function(e){
	        	var id = event.target.id.slice(6);
	        	var widget = this;
	        	var time = moment(Number(id)).startOf("day").valueOf();
	        	Dialog.alert({
					content : "确定要清空菜单吗？",
					confirm:function(){
						aw.ajax({
							url:"api/cate/dishmenu/removeoneday",
							data:{
								"menuDate":time,
							},
							dataType:"json",
							success:function(data){
								events.setData(widget,moment(time));
								//window.location.reload();
								// $("#tableId").refresh();
							}
						});
						Dialog.close();
					}
				});
	        }
        },
        initComponent: function (params, widget) {
        	dishMenuSubnavComp.initComponent(params, widget);
        },
        afterInitComponent: function (params, widget) {
           events.setData(widget,date);
        }
    });
   

    module.exports = App;
});
